﻿package com.example.sina;



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.net.RequestListener;

public class MainActivity extends Activity implements WeiboAuthListener, OnClickListener {
	private Weibo mWeibo;
	private static final String Consumer_Key = "1008362046";// 替换为开发者的appkey，例如"1646212860";
	private static final String Redirect_URL = "http://www.baidu.com";
	public static Oauth2AccessToken accessToken;
	private Button login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		mWeibo = Weibo.getInstance(Consumer_Key, Redirect_URL);
		login = (Button) findViewById(R.id.login);
		login.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) { // 请求登录
		mWeibo.authorize(MainActivity.this, this);
	}

	@Override
	public void onCancel() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onComplete(Bundle bundle) {
		Log.d("onComplete", bundle.toString());
		String uid = bundle.getString("uid");
		String access_token = bundle.getString("access_token");
		String expires_in = bundle.getString("expires_in");

		accessToken = new Oauth2AccessToken(access_token, expires_in);
		UsersAPI userInfo = new UsersAPI(accessToken);
		userInfo.show(uid, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				e.printStackTrace();
				Log.d("onIOException", "onIOException");
			}

			@Override
			public void onError(WeiboException e) {
				e.printStackTrace();
			}

			@Override
			public void onComplete(String values) {
				Log.d("userInfo", values);
			}
		});
//		StatusesAPI stat = new StatusesAPI(accessToken);
//		stat.publicTimeline(10, 10, true, new RequestListener() {
//
//			@Override
//			public void onIOException(IOException arg0) {
//				arg0.printStackTrace();
//			}
//
//			@Override
//			public void onError(WeiboException arg0) {
//				arg0.printStackTrace();
//			}
//
//			@Override
//			public void onComplete(String arg0) {
//				Log.d("StatusesAPI", arg0);
//			}
//		});
	}

	@Override
	public void onError(WeiboDialogError arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onWeiboException(WeiboException arg0) {
		// TODO Auto-generated method stub

	}

}
